package com.lishe.lishe.lishe.module.main.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.module.main.bean.ApkMessage;

/**
 * Created by DELL on 2017/10/30.
 */

public class ShowUpdateDialog extends Dialog {


    public ShowUpdateDialog(@NonNull Context context){
        super(context);
    }

    public ShowUpdateDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    public static class Builder implements View.OnClickListener {
        private Context context;
        private ShowUpdateDialog dialog;

        private ApkMessage apkMessage;
        private UpdateListener listener;
        private TextView update;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setApkMessage(ApkMessage apkMessage){
            this.apkMessage=apkMessage;
            return  this;
        }

        public Builder setUpdateListener(UpdateListener listener){
            this.listener=listener;
            return this;
        }

        public ShowUpdateDialog create() throws NullPointerException {
            if (dialog == null) {
                dialog = new ShowUpdateDialog(context,R.style.MyTransparentDialog);
                View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_show_update, null);
                TextView update_content=contentView.findViewById(R.id.update_content);
                update=contentView.findViewById(R.id.update);
                ImageView dismiss=contentView.findViewById(R.id.dismiss);
                update.setOnClickListener(this);
                dismiss.setOnClickListener(this);

                update_content.setText(apkMessage.getApp_note());

                dialog.setContentView(contentView);
                dialog.setCanceledOnTouchOutside(false);
                dialog.setCancelable(false);
            }
            return dialog;
        }

        public void show() {
            try {
                create().show();
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }

        public void dissmiss() {
            if (dialog != null) {
                dialog.dismiss();
                dialog.cancel();
                dialog=null;
            }
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.update:
                    if (apkMessage.getApp_update().equals("1")) {//强制更新
                        update.setText("新版礼舍更新中...");
                    }
                    listener.update();
                    break;
                case R.id.dismiss:
                    if (!apkMessage.getApp_update().equals("1")) {//强制更新
                        listener.dismiss();
                    }
                    break;
            }
        }
    }

    public interface UpdateListener{
        void update();
        void dismiss();
    }

}
